﻿using Manager.Model;
using Manager.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Manager.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceController : ControllerBase
    {
        private readonly PhoneService _phoneService;
        public DeviceController(PhoneService phoneService)
        {
            _phoneService = phoneService;
        }


        [HttpGet("id")]
        public ObjectResult GetById(int id)
        {
            var phone = _phoneService.GetPhone(id);
            return new ObjectResult(new { success = true, data = phone });
        }

        [HttpGet("name")]
        public ObjectResult GetByName(string? name = "")
        {
            var phones = _phoneService.GetPhoneByName(name);
            return new ObjectResult(new { success = true, data = phones });
        }

        [HttpPost]
        public ObjectResult AddPhone(Phone phone)
        {
            _phoneService.AddPhone(phone);
            return new ObjectResult(new { success = true });
        }

        [HttpDelete]
        public ObjectResult DeletePhone(int id)
        {
            _phoneService.DeletePhone(id);
            return new ObjectResult(new { success = true });
        }

        [HttpPut]
        public ObjectResult UpdatePhone(Phone phone)
        {
            _phoneService.UpdatePhone(phone);
            return new ObjectResult(new { success = true });
        }
    }
}
